package com.crm.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.pojo.Backmoneyplan;
import com.crm.model.pojo.Customer;
import com.crm.model.pojo.Orderinfo;
import com.crm.services.BackmoneyplanService;
import com.crm.services.CustomerServices;
import com.crm.services.OrderInfoServers;
import com.crm.services.UserServices;
import com.github.pagehelper.PageInfo;

/**
 * 回款业务类
 * 
 * @author 周远星
 */
@Controller
public class BackmoneyplanController {
	@Autowired
	BackmoneyplanService bmpService;
	@Autowired
	UserServices userServer;
	@Autowired
	OrderInfoServers orderServer;

	@Autowired
	CustomerServices cumService;

	/**
	 * 分页跳转到订单页
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/sales/prosale/backMoneyPlan")
	public ModelAndView order(Integer pageNum, Integer pageSize,String statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("sales/prosale/backMoneyPlan");
		// 分页查询订单
		List<Map<String, String>> findAllMaper = bmpService.selectByPager(pageNum, pageSize,statusid,search);
		System.out.println(findAllMaper);
		String jsonString = JSONObject.toJSONString(findAllMaper);
		// 获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(findAllMaper);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);
		return mv;
	} 
	/**
	 * 分页查询回款计划
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/sales/prosale/backMoneyPlan")
	@ResponseBody	
	public PageInfo<Map<String, String>> backMoneyPlan(Integer pageNum, Integer pageSize,String statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = bmpService.selectByPager(pageNum, pageSize,statusid,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}
	
	
	
	@PostMapping("/sales/prosale/addbmp")
	@ResponseBody
	public String addOrder(@RequestParam(name = "comcustid", required = false) Integer comcustid,
			@RequestParam(name = "orderid", required = false) Integer orderid,
			@RequestParam(name = "date1", required = false) String date1,
			@RequestParam(name = "bmpmoney", required = false) String bmpmoney,
			@RequestParam(name = "radiosatus", required = false) String radiosatus,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam(name = "textcommunication", required = false) String textcommunication) {
		// 根据ID查询客户
		Customer customer = cumService.findByCustIdPage(comcustid);
		Orderinfo orderinfo = orderServer.findById(orderid);
		Backmoneyplan bmp = new Backmoneyplan();
		bmp.setCustomer(customer);
		bmp.setOrderinfo(orderinfo);
		bmp.setBmpBmpdate(java.sql.Date.valueOf(date1));
		bmp.setBmpMoney(new BigDecimal(bmpmoney));
		bmp.setBmp_isBackMoney(radiosatus);
		bmp.setUId(uid);
		bmp.setBmpForeign(textcommunication);
		bmp.setBmpStatus(1102);
		Backmoneyplan addBmp = bmpService.addBmp(bmp);
		System.out.println(addBmp);
		return "新增回款计划";
	}

	/**
	 * 用于查询加载回款计划界面数据
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/addbmppage")
	@ResponseBody
	public JSONObject addOrderpage() {
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = cumService.selectAllCustomer();
		// 用于加载所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("userAll", selectUserAll);
		return jsonarray;
	}

	/**
	 * 用于查询加载回款记录界面数据订单
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/bmporderpage")
	@ResponseBody
	public JSONObject bmporderpage(@RequestParam(name = "ID") Integer cumid) {
		// 用于加载所有者信息
		List<Map<String, String>> selectAllBypageID = orderServer.selectAllBypageID(cumid);
		JSONObject jsonarray = new JSONObject();
		System.out.println(selectAllBypageID);
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = cumService.selectAllCustomer();
		// 用于加载所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("userAll", selectUserAll);
		jsonarray.put("order", selectAllBypageID);
		return jsonarray;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/sales/prosale/deletebmp")
	@ResponseBody
	public String dateleOrder(@RequestParam("ID") Integer id) {
		// 调用删除
		bmpService.deleteBmp(id);
		return "删除";

	}

	/**
	 * 点击跳转修改页，用于界面的数据显示
	 * 
	 * @param comcustid
	 * @return
	 */
	@PostMapping("/sales/prosale/updatebmppage")
	@ResponseBody
	public JSONObject updatebmppage(@RequestParam("ID") Integer comcustid) {
		// 加载页面数据
		List<Map<String, String>> findById = bmpService.findById(comcustid);
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = cumService.selectAllCustomer();
		// 用于加载所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("userAll", selectUserAll);
		jsonarray.put("bmpform", findById);
		return jsonarray;
	}
	
	/**
	 *修改回款计划
	 * @param comcustid
	 * @param orderid
	 * @param date1
	 * @param bmpmoney
	 * @param radiosatus
	 * @param uid
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/sales/prosale/updatebmp")
	@ResponseBody
	public String updateBmp(
			@RequestParam(name = "comcustid", required = false) Integer comcustid,
			@RequestParam(name = "orderid", required = false) Integer orderid,
			@RequestParam(name = "bmpbmpdate", required = false) String bmpbmpdate,
			@RequestParam(name = "bmpmoney", required = false) String bmpmoney,
			@RequestParam(name = "isBackMoney", required = false) String isBackMoney,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam("ID") Integer id,
			@RequestParam(name = "textcommunication", required = false) String textcommunication) {
		// 根据ID查询客户
		Customer customer = cumService.findByCustIdPage(comcustid);
		Orderinfo orderinfo = orderServer.findById(orderid);
		Backmoneyplan bmp = new Backmoneyplan();
		bmp.setBmpId(id);
		bmp.setCustomer(customer);
		bmp.setOrderinfo(orderinfo);
		bmp.setBmpBmpdate(java.sql.Date.valueOf(bmpbmpdate.substring(0, 10)));
		bmp.setBmpMoney(new BigDecimal(bmpmoney));
		bmp.setBmp_isBackMoney(isBackMoney);
		bmp.setUId(uid);
		bmp.setBmpForeign(textcommunication);
		bmp.setBmpStatus(1102);
		Backmoneyplan addBmp = bmpService.addBmp(bmp);
		System.out.println(addBmp);
		return "修改回款计划";
	}
	
	/**
	 * 用于智能添加回款计划
	 * @return 
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/addbmpitemview")
	@ResponseBody
	public String addorderitemview(@RequestParam("bmpitem") Object proid) {
		String str= String.valueOf(proid);
		@SuppressWarnings("unchecked")
		List<Map<String,String>> limap = JSONArray.parseObject(str,List.class);
		bmpService.addbmpitemview(limap);
		return "123";
	}
	
}
